CREATE PROCEDURE spAPI_User_Register
(
	@LoginName VARCHAR(50),
	@Password VARCHAR(50),
	
	@Email VARCHAR(50),
	@Phone VARCHAR(50),
	@ZipCode VARCHAR(5),
	@ReferredBy VARCHAR(100)
)
AS
BEGIN
	SET NOCOUNT ON
	SET XACT_ABORT ON
	
	DECLARE @SecurityToken UNIQUEIDENTIFIER
	
	BEGIN TRANSACTION
		INSERT INTO UserBusinessLogins
		(LoginName, LoginType)
		VALUES (@LoginName, 'U')
		
		INSERT INTO [User]
		([Login], Email, Phone, IsActive, [Password], Deleted, ZipCode, ReferredBy, RegState, RegCity)
		VALUES (@LoginName, @Email, @Phone, 1, @Password, 0, @ZipCode, @ReferredBy, '', '')
		
		SET @SecurityToken = NEWID()
					
		INSERT INTO UserToken
		(SecurityToken, LoginName, ExpirationDate)
		VALUES(@SecurityToken, @LoginName, '99991231')
		
		SELECT @SecurityToken AS SecurityToken
	COMMIT TRANSACTION
END	